/**
 * Created by thinkl on 2017/8/27.
 */
$(function () {
    var address_data = {
        baseConfig: baseConfig,
        address_list: [],
        address_default: null,
        selected_address_id: parseInt(GetQueryString('address_id')),
        payorder_id: parseInt(GetQueryString("payorder_id")),
    };
    var address_list = new Vue({
        el: '#address_list',
        data: address_data,
        methods: {
            choice_address: function (payorder_id, address_id) {
                if (payorder_id) {
                    api.lorder.update_pay_order_address({
                        order_id: payorder_id,
                        address_id: address_id
                    }).then(function (data) {
                        location.href = 'pay.html?payorder_id=' + payorder_id;
                        // history.go(-1);
                    });
                }

            },
            create_address: function () {
                location.href = 'address_modefy.html';
            },
            modefy_address: function (address_id) {
                location.href = 'address_modefy.html?payorder_id=' + address_data.payorder_id + '&address_id=' + address_id;
            },
            set_default: function (address_default) {
                api.lorder.update_default_address({address_id: address_default}).then(function (data) {

                });
            },
            delete_address: function (address) {
                Magiclamp.mcConfirm("提示", "是否删除收件地址？", null, function () {
                    api.lorder.delete_address({address_id: address.id}).then(function (data) {
                        address.is_active = false;
                    })
                });

            },
            page_back: function () {
                baseConfig.page_back();
            }
        }
    });


    document.getElementsByTagName("BODY")[0].onpageshow = function (event) {
        if(event && !event.persisted){
            return;
        }
        api.lorder.query_address_list().then(function (data) {
            address_data.address_list = data.result.list;
            _(data.result.list).each(function (item) {
                if (item.is_default) {
                    address_data.default_address = item.id;
                }
            })
        });
    };
    document.getElementsByTagName("BODY")[0].onpageshow();
});